/*
 * GetFilelistConnection.java
 *
 * Created on 22 September 2006, 11:16
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package hubsniffer.conn;

import libjdc.dc.client.conn.DCFileDownload;
import libjdc.dc.protocol.Protocol;
import hubsniffer.DCHub;
import libjdc.dc.client.User;
import libjdc.dc.log.Log;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.Date;

/**
 *
 * @author root
 */
public class GetFilelistConnection_1 extends HubConnection{
    
    /** Creates a new instance of GetFilelistConnection */
    public GetFilelistConnection_1(DCHub hub,User me) {
        super(hub,me);
    }
    
    protected void handlerMyInfo(String myInfo) throws ParseException{
        Log.out.println(new Date() +" :: GetFilelistConnection_1.handlerMyInfo");
        Object[] obj = Protocol.MYINFO_PARSER.parse(myInfo);
        
        if(obj[0].toString().equals(user.getUserInfo().getNick())){
            hub.getHubSession().getUserList().add(user);
            return;
        }
        
        User newUser = new User(obj[0] + "", obj[1] + "", obj[2] + "", obj[3]
                + "", new Long(obj[4].toString()).longValue());
        hub.getHubSession().getUserList().add(newUser);
        if(!newUser.equals(user))
            newUser.setHubConnection(this);
    }
    
    public void sendConnectToMe(User other) throws UnsupportedEncodingException, IOException, InterruptedException{
        Log.out.println(new Date() +" :: GetFilelistConnection_1.sendConnectToMe");
        DCFileDownload conn = new DCFileDownload(user,other,"files.xml.bz2","/home/dbotelho/lists/");
       conn.start();
        /* DCUserConnection conn = new DCUserConnection(user,other);
        conn.connect();*/
        Object[] obj ={ other.getUserInfo().getNick(),conn.getConnection().getHost(),conn.getConnection().getPort()+""};
        
        sendRaw( Protocol.CONNECTTOME_PARSER.format(obj) );
    }
}
